package com.teamwhole.modules.sys.service;

import java.util.Date;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.teamwhole.common.config.Global;
import com.teamwhole.common.utils.CacheUtils;
import com.teamwhole.common.utils.StringUtils;
import com.teamwhole.core.persistence.service.CrudService;
import com.teamwhole.core.web.Servlets;
import com.teamwhole.modules.sys.dao.UserDao;
import com.teamwhole.modules.sys.entity.User;
import com.teamwhole.modules.sys.utils.UserUtils;

@Service
public class UserService extends CrudService<UserDao, User>{
	
	public User get(String id) {
		return UserUtils.get(id);
	}
	
	public User findByLoginName(String loginName) {
		return UserUtils.findByLoginName(loginName);
	}
	
	@Transactional(readOnly = false)
    public void modifyPassword(User user) {
        String pass = SystemService.entryptPassword(user.getPassword());
        user.setPassword(pass);
        this.dao.initPassword(user);
    }
	
	@Transactional(readOnly = false)
	public void initPassword(User user) {
	    // password setting
		if (StringUtils.isBlank(user.getPassword())) {
        	String newPass = Global.getConfig("init.password");
        	if (StringUtils.isBlank(newPass)) {
        		newPass = "1234";
        	}
            user.setPassword(newPass);
        }
        String pass = SystemService.entryptPassword(user.getPassword());
        user.setPassword(pass);
        this.dao.initPassword(user);
	}
	
	@Transactional(readOnly = false)
	public void save(User user) {
	    // password setting
		if (StringUtils.isBlank(user.getPassword())) {
		    user.setPassword("1234");
		    String pass = SystemService.entryptPassword(user.getPassword());
	        user.setPassword(pass);
		}
		if (StringUtils.isBlank(user.getId())) {
			user.preInsert();
			this.dao.insert(user);
		} else {
			// 清除原用户机构用户缓存
			User oldUser = this.dao.get(user.getId());
			if (oldUser.getDept() != null && oldUser.getDept().getId() != null) {
				CacheUtils.remove(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LIST_BY_DEPT_ID_ + oldUser.getDept().getId());
			}
			// 更新用户数据
			user.preUpdate();
			this.dao.update(user);
		}
		if (StringUtils.isNotBlank(user.getId())) {
			// 更新用户与角色关联
			this.dao.deleteUserRole(user);
			if (user.getRoleList() != null && user.getRoleList().size() > 0) {
				this.dao.insertUserRole(user);
			} else {
				//throw new RuntimeException(user.getLoginName() + "没有设置角色！");
			}
			// 清除用户缓存
			UserUtils.clearCache(user);
			// 清除权限缓存
			//systemRealm.clearAllCachedAuthorizationInfo();
		}
	}
	
	@Transactional(readOnly = false)
	public void updateUserInfo(User user) {
		user.preUpdate();
		this.dao.update(user);
		// 清除用户缓存
		UserUtils.clearCache(user);
		// 清除权限缓存
        //systemRealm.clearAllCachedAuthorizationInfo();
	}

	@Transactional(readOnly = false)
	public void delete(String id) {
		if (!id.equals("1")) {
			// userDao.deleteUserRole(user);
			User user = UserUtils.get(id);
			this.dao.delete(user);
			// 清除用户缓存
			UserUtils.clearCache(user);
		}
	}

	@Transactional(readOnly = false)
	public void updateUserLoginInfo(User user) {
		// 更新本次登录信息
		user.setLoginIp(StringUtils.getRemoteAddr(Servlets.getRequest()));
		user.setLoginDate(new Date());
		this.dao.updateLoginInfo(user);
	}
	
}
